#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "clip.h"
#include "ui_clip.h"
#include <QDesktopWidget>
#include <QFileDialog>
#include <QTime>
#include <QMessageBox>
#include <QShortcut>
#include <QDesktopServices>
#include <QDragEnterEvent>
#include <QMimeData>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    filepath = ".";
    ui->setupUi(this);
    move((QApplication::desktop()->width() - width())/2, (QApplication::desktop()->height() - height())/2);

    video = new QVideoWidget;
    video->setStyleSheet("background:black;");
    ui->layoutVideo->addWidget(video);
    video->setMouseTracking(true);
    video->show();
    player = new QMediaPlayer;    
    player->setVolume(100);
    player->setVideoOutput(video);
    connect(player, SIGNAL(durationChanged(qint64)), this, SLOT(durationChange(qint64)));
    connect(player, SIGNAL(positionChanged(qint64)), this, SLOT(positionChange(qint64)));
    connect(player, SIGNAL(stateChanged(QMediaPlayer::State)), SLOT(stateChange(QMediaPlayer::State)));
    connect(ui->sliderProgress, SIGNAL(valueChanged(int)), this, SLOT(setSTime(int)));
    connect(ui->sliderProgress, SIGNAL(sliderReleased()), this, SLOT(setMPPosition()));
    connect(new QShortcut(QKeySequence(Qt::Key_Space), this), SIGNAL(activated()), this, SLOT(on_pushButtonPlay_clicked()));
    connect(new QShortcut(QKeySequence(Qt::Key_Left), this), SIGNAL(activated()), this, SLOT(seekBackward()));
    connect(new QShortcut(QKeySequence(Qt::Key_Right), this), SIGNAL(activated()), this, SLOT(seekForward()));
    connect(new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Left), this), SIGNAL(activated()), this, SLOT(seekBackward1()));
    connect(new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Right), this), SIGNAL(activated()), this, SLOT(seekForward1()));
    connect(ui->listWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(playClip(QListWidgetItem*)));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::open(QString path)
{
    filepath = path;
    player->setMedia(QUrl::fromLocalFile(path));
    player->play();
    ui->statusBar->showMessage("打开 " + path);
    ui->sliderProgress->setStyleSheet("");
    ui->listWidget->clear();
}

void MainWindow::on_action_open_triggered()
{
    //if(filepath == "") filepath = ".";
    filepath = QFileDialog::getOpenFileName(this, "打开媒体文件", filepath);
    if (!filepath.isEmpty()) {
        open(filepath);
    }
}

void MainWindow::on_action_open_url_triggered()
{
    QDialog *dialog = new QDialog(this);
    dialog->setFixedSize(300,100);
    dialog->setWindowTitle("打开网址");
    QVBoxLayout *vbox = new QVBoxLayout;
    QLineEdit *lineEdit = new QLineEdit;
    vbox->addWidget(lineEdit);
    QHBoxLayout *hbox = new QHBoxLayout;
    hbox->addStretch();
    QPushButton *pushButton_confirm = new QPushButton("确定");
    hbox->addWidget(pushButton_confirm);
    QPushButton *pushButton_cancel = new QPushButton("取消");
    hbox->addWidget(pushButton_cancel);
    hbox->addStretch();
    vbox->addLayout(hbox);
    dialog->setLayout(vbox);
    connect(pushButton_confirm, SIGNAL(clicked()), dialog, SLOT(accept()));
    connect(pushButton_cancel, SIGNAL(clicked()), dialog, SLOT(reject()));
    if (dialog->exec() == QDialog::Accepted) {
        filepath = lineEdit->text();
        player->setMedia(QUrl(filepath));
        player->play();
        ui->statusBar->showMessage("打开 " + filepath);
        ui->sliderProgress->setStyleSheet("");
        ui->listWidget->clear();
        dialog->close();
    }
}

void MainWindow::on_action_about_triggered()
{
    QMessageBox aboutMB(QMessageBox::NoIcon, "关于", "海天鹰视频分割器 1.1\n一款基于 Qt 和 FFmpeg 的视频分割器。\n作者：海天鹰\nE-mail: sonichy@163.com\n主页：https:/gitee.com/sonichy/HTYVS");
    aboutMB.setIconPixmap(QPixmap(":/HTYVS.jpg").scaled(150,150));
    aboutMB.exec();
}

void MainWindow::on_pushButtonPlay_clicked()
{
    if (player->state() == QMediaPlayer::PlayingState) {
        player->pause();
    } else {
        player->play();
    }
}

void MainWindow::on_pushButtonStart_clicked()
{
    msStart = ui->sliderProgress->value();
    areaChange(msStart, msEnd);
    if (ui->listWidget->currentRow() != -1) {
        Clip *clip = (Clip*)(ui->listWidget->itemWidget(ui->listWidget->item(ui->listWidget->currentRow())));
        QTime t(0,0,0);
        t = t.addMSecs(msStart);
        clip->ui->timeEditStart->setTime(t);
    }
}

void MainWindow::on_pushButtonEnd_clicked()
{
    msEnd = ui->sliderProgress->value();
    areaChange(msStart, msEnd);
    if (ui->listWidget->currentRow() != -1) {
        Clip *clip = (Clip*)(ui->listWidget->itemWidget(ui->listWidget->item(ui->listWidget->currentRow())));
        QTime t(0,0,0);
        t = t.addMSecs(msEnd);
        clip->ui->timeEditEnd->setTime(t);
    }
}

void MainWindow::areaChange(int start, int end)
{
    float p1 = (float)start/ui->sliderProgress->maximum();
    float p2 = (float)end/ui->sliderProgress->maximum();
    ui->sliderProgress->setStyleSheet(QString("background-color: qlineargradient( spread:pad, x1:0, y1:0, x2:1, y2:0,"
                                              "stop:0 #FFFFFF, stop:%1 #FFFFFF,"
                                              "stop:%2 #0000FF, stop:%3 #0000FF,"
                                              "stop:%4 #FFFFFF, stop:1 #FFFFFF );")
                                      .arg(p1-0.001)
                                      .arg(p1)
                                      .arg(p2-0.001)
                                      .arg(p2));
}

void MainWindow::on_pushButtonAdd_clicked()
{
    Clip *clip = new Clip;
    connect(clip, SIGNAL(areaChange(int,int)), this, SLOT(areaChange(int,int)));
    connect(clip, SIGNAL(statusBarMessage(QString)), ui->statusBar, SLOT(showMessage(QString)));
    clip->filepath = filepath;
    QDateTime datetime = QDateTime::currentDateTime();
    clip->clipname = "clip" + datetime.toString("yyyyMMddhhmmss");
    QTime t(0,0,0);
    t = t.addMSecs(ui->sliderProgress->maximum());
    clip->ui->timeEditStart->setMaximumTime(t);
    clip->ui->timeEditEnd->setMaximumTime(t);
    t.setHMS(0,0,0);
    t = t.addMSecs(msStart);
    clip->ui->timeEditStart->setTime(t);
    t.setHMS(0,0,0);
    t = t.addMSecs(msEnd);
    clip->ui->timeEditEnd->setTime(t);
    t.setHMS(0,0,0);
    t = t.addMSecs(msEnd - msStart);
    clip->ui->timeEditDuration->setTime(t);
    QListWidgetItem *LWI = new QListWidgetItem(ui->listWidget);
    LWI->setSizeHint(QSize(500,32));
    ui->listWidget->addItem(LWI);
    ui->listWidget->setItemWidget(LWI,clip);
}

void MainWindow::on_pushButtonDelete_clicked()
{
    if (ui->listWidget->currentRow() != -1) {
        ui->listWidget->takeItem(ui->listWidget->currentRow());
        ui->listWidget->setCurrentRow(-1);
    }
}

void MainWindow::on_pushButtonDirectory_clicked()
{
    if (filepath != "") {
        QDesktopServices::openUrl(QUrl::fromLocalFile(QFileInfo(filepath).absolutePath()));
    }
}

void MainWindow::durationChange(qint64 d)
{
    ui->sliderProgress->setMaximum(d);
    QTime t(0,0,0);
    t = t.addMSecs(d);
    QString sTimeTotal = t.toString("hh:mm:ss");
    ui->labelTimeTotal->setText(sTimeTotal);
    msStart = 0;
    //msEnd = 0;//崩溃
    msEnd = d;
}

void MainWindow::positionChange(qint64 p)
{
    ui->sliderProgress->setValue(p);
    //qDebug() << p << msEnd; //崩溃
    //if (p > msEnd) player->pause();
}

void MainWindow::setSTime(int v)
{
    QTime t(0,0,0);
    t = t.addMSecs(v);
    QString sTimeElapse = t.toString("hh:mm:ss");
    ui->labelTimeNow->setText(sTimeElapse);
}

void MainWindow::setMPPosition()
{
    player->setPosition(ui->sliderProgress->value());
}

void MainWindow::stateChange(QMediaPlayer::State state)
{
    //qDebug() << state;
    if(state == QMediaPlayer::PlayingState){
        ui->pushButtonPlay->setIcon(QIcon::fromTheme("media-playback-pause"));
        ui->pushButtonPlay->setToolTip("暂停 Space");
    }else if(state == QMediaPlayer::PausedState){
        ui->pushButtonPlay->setIcon(QIcon::fromTheme("media-playback-start"));
        ui->pushButtonPlay->setToolTip("播放 Space");
    }else if(state == QMediaPlayer::StoppedState){
        ui->pushButtonPlay->setIcon(QIcon::fromTheme("media-playback-start"));
        ui->pushButtonPlay->setToolTip("播放 Space");
    }
}

void MainWindow::playClip(QListWidgetItem* item)
{
    Clip *clip = (Clip*)(ui->listWidget->itemWidget(item));
    msStart = clip->ui->timeEditStart->time().msecsSinceStartOfDay();
    msEnd = clip->ui->timeEditEnd->time().msecsSinceStartOfDay();
    player->setPosition(msStart);
    player->play();
}

void MainWindow::seekBackward()
{
    player->setPosition(player->position() - 5000);
}

void MainWindow::seekForward()
{
    player->setPosition(player->position() + 5000);
}

void MainWindow::seekBackward1()
{
    player->setPosition(player->position() - 1000);
}

void MainWindow::seekForward1()
{
    player->setPosition(player->position() + 1000);
}

void MainWindow::dragEnterEvent(QDragEnterEvent *e)
{
    //if(e->mimeData()->hasFormat("text/uri-list")) //只能打开文本文件
        e->acceptProposedAction(); //可以在这个窗口部件上拖放对象
}

void MainWindow::dropEvent(QDropEvent *e) //释放对方时，执行的操作
{
    QList<QUrl> urls = e->mimeData()->urls();
    if(urls.isEmpty())
        return ;

    QString fileName = urls.first().toLocalFile();

//    foreach (QUrl u, urls) {
//        qDebug() << u.toString();
//    }
//    qDebug() << urls.size();

    if(fileName.isEmpty())
        return;

    open(fileName);
}